package cn.itcast;


import cn.itcast.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class demo {
    private InputStream is;
    private SqlSession session;
    @Before
    public void before() throws IOException {
        //Resources工具类读取核心配置文件
        is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //构建SqlSessionFactory工厂对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取sqlsession对象,openSession()方法的参数默认为false(空)手动提交事务，可设置为true，表示自动提交事务
        session = factory.openSession();
    }
    @After
    public void after() throws IOException {
        session.commit();
        session.close();
        is.close();
    }
    @Test
    public void findAll() {
        List<User> list = session.selectList("UserMapper.selectAll");
        System.out.println(list.size());
    }
    @Test
    public void findById() {
        User user = session.selectOne("UserMapper.selectById", 2);
        System.out.println(user);
    }
    @Test
    public void findByUser() {
        User u = new User();
        u.setId(1);
        User user = session.selectOne("UserMapper.selectByUser", u);
        System.out.println(user);
    }
   /* @Test (存在问题)
    public void findByLike() {
       *//* User u = new User();
        u.setUsername("李");*//*
        List<User> list = session.selectList("UserMapper.selectByLike", "d");
        for (User user : list) {
            System.out.println(user);
        }
    }*/

    @Test
    public void updateById() {
        User u = new User(1,"admind","123654");
        int count = session.update("UserMapper.update", u);
        System.out.println(count);
    }
    @Test
    public void deleteById() {
        int count = session.delete("UserMapper.delete", 2);
        System.out.println(count);
    }
    @Test
    public void insert() {
        User u = new User(2,"lisi","654987");
        int count = session.insert("UserMapper.insert", u);
        System.out.println(count);
    }

    @Test
    public void insert1() {
        User u = new User(3,"李晓明","654987");
        int count = session.insert("UserMapper.insert", u);
        System.out.println(count );
    }

}
